﻿//系统管理-->用户组管理
//Create By:Cui 2014年9月17日11:41:41

//1.0 加载JqueryDatabase
jQuery(function ($) {
    var oTable1 = $('#sample-table-2').dataTable({
        "aoColumns": [
         { "bSortable": false },
         { "bSortable": true },
         { "bSortable": true },
         { "bSortable": false },
         { "bSortable": true },
         { "bSortable": false },
        ],
        oLanguage: {
            "sProcessing": "正在加载中......",
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "对不起，查询不到相关数据！",
            "sEmptyTable": "表中无数据存在！",
            "sInfo": "当前显示： _START_ 到 _END_ 条，共 _TOTAL_ 条",
            "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
            "sSearch": "",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "末页"
            }
        },
        iDisplayLength: 100,
        bLengthChange: false
    });


    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
        .each(function () {
            this.checked = that.checked;
            $(this).closest('tr').toggleClass('selected');
        });

    });


    $('[data-rel="tooltip"]').tooltip({ placement: tooltip_placement });
    function tooltip_placement(context, source) {
        var $source = $(source);
        var $parent = $source.closest('table')
        var off1 = $parent.offset();
        var w1 = $parent.width();

        var off2 = $source.offset();
        var w2 = $source.width();

        if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
        return 'left';
    }


})

//$(function () {
//    $("div .page-header h1").attr("style", "text-align:left");

//});

window.onload = function () {
    //2.0 页面初始化
    //2.1 给table加按钮（增加，删除，刷新）
    //var $aa = $("#sample-table-2_wrapper .col-sm-6").eq(0);
    //$aa.html("<button class='btn btn-primary btn-sm' id='butadd'   ><i class='icon-plus-sign bigger-110'></i><span class='bigger-110 no-text-shadow'>新增</span></button>");
    //$aa.append("<button class='btn btn-danger btn-sm' style='margin-left: 4px' id='butdel'><i class='icon-trash bigger-110'></i><span class='bigger-110 no-text-shadow'>删除</span></button>");
    //$aa.append("<button class='btn btn-success btn-sm' style='margin-left: 4px' id='buflush'><i class='icon-refresh bigger-110'></i><span class='bigger-110 no-text-shadow'>刷新</span></button>");
   // $("td").addClass("tdBorder");


    //2.2 table表头上的样式
    $("#sample-table-2>thead tr>th").attr("style", "font-weight:bold;color:black;text-align:center");


    //2.3 table搜索样式
    $("#sample-table-2_filter label input").attr("placeholder", "搜索(多条件搜索中间用空格隔开)").attr("style", "width:50%");
    $("#sample-table-2_filter label").attr("style", "width:100%");
    
    $("#sample-table-2_filter").attr("style", "margin-right: -16px;");
    $("#sample-table-2_wrapper .row").first().children().eq(1).attr("style", "margin-top:-12px");
    $("#sample-table-2_wrapper").attr("style", "margin-top:-32px");
    $("#butadd").attr("style", "margin-top:-4px");

    //2.4 增加表单行间距样式
    //$("div .modal-body .row:not(#iconr)").attr("style", "margin-top:-14px;");
    $("div .modal-body .row .col-sm-3").attr("style", "margin-top:6px;text-align:right");
    $("div#myModal .modal-body .row i:lt(3)").attr("style", "margin-top:9px;text-align:right;color:gray");

    //2.5 显示页数一行的样式
    $("#sample-table-2").next().find(".col-sm-6").eq(0).attr("style", "margin-left: -12px;margin-top: 5px;");
    $("#sample-table-2").next().find(".col-sm-6").eq(1).attr("style", "margin-left: 12px;margin-top: 0px;");

    //2.6 按钮样式
    $("button.btn.btn-sm:gt(0)").attr("style", "margin:-5px 0px 0px 4px");

    //3.0 功能操作
    //3.1 点击新增按钮，打开模态窗口
    function addInit() {
        $("#userGroupName").val("");
        $("#Remark").val("");
        $("#but_saveadd").removeAttr("style");
        $("#but_savemodify").attr("style", "display:none");
    }

    $("#butadd").click(function () {
        addInit();
        $("#myModal").modal();
    });
    //3.1 新增保存
    $("#but_saveadd").click(function () {
        var userGroupName = $("#userGroupName").val();
        var Remark = $("#Remark").val();
        if (userGroupName.trim().length==0) {
            $.gritter.add({
                title: '消息提醒',
                text: "用户组名不能为空！",
                class_name: 'gritter-info gritter-center'
            });
            return;
        }
        $.ajax({
            url: "AddUserGroup",
            dataType: "json",
            data: {
                userGroupName: userGroupName,
                Remark:Remark
            },
            Type: "post",
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    $("#but_infoclose").click();
                    setTimeout(function () {
                        history.go(0)
                    }, 3000);
                   
                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }

            }
        });
    });

    //3.2 刷新
    $("#buflush").click(function () {
        history.go(0);
    });
    //遮罩层div
    $("#bgdiv").attr("style", "display:none");
    //3.3 删除
    $("#butdel").click(function () {
        var idString = "";
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function () {
            idString += $(this).text() + ","
        });
        idString = idString.substring(0, idString.length - 1);
        if (idString.length == 0) {
            $.gritter.add({
                title: '消息提醒',
                text: '请选择要删除的项',
                class_name: 'gritter-info gritter-center'
            });
        }
        else {
            if (!confirm("确定删除")) { return; }
            $.ajax({
                url: "DeleteUserType",
                dataType: "Json",
                data: { idString: idString },
                Type: "Post",
                success: function (data) {
                    var json = eval(data);
                    if (json.result == "Success") {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                        setTimeout(function () {
                            history.go(0)
                        }, 3000);
                    }
                    else {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                    }
                }
            });
        }
    });


}

function addInit() {
    $("#userGroupName").val("");
    $("#Remark").val("");
    $("#but_saveadd").removeAttr("style");
    $("#but_savemodify").attr("style", "display:none");
}
//3.4 修改详情
 


$(".showModify").click(function () {
//$("#sample-table-2 a.modify11").click(function () {
    //改成下拉列表多了一个parent（）
    $("#but_saveadd").attr("style", "display:none");
    $("#but_savemodify").removeAttr("style"); 
    var id = $(this).parent().parent().parent().children().eq(1).text()
    $.ajax({
        url: "ShowInfo",
        datatype: "json",
        type: "post",
        data: {
            id: id
        },
        success: function (data) {
            var json = eval(data);
            $("#userGroupName").val(json.userGroupName);
            $("#Remark").val(json.Remark);
            $("#idHidden").val(json.userGroupID);
        }
    });
    $("#myModal").modal();
});
//5.6.1 修改保存
$("#but_savemodify").click(function () {
    var userGroupID = $("#idHidden").val();
    var Remark = $("#Remark").val();
    var userGroupName = $("#userGroupName").val();
    if (userGroupName.trim().length == 0) {
        $.gritter.add({
            title: '消息提醒',
            text: "用户组名不能为空！",
            class_name: 'gritter-info gritter-center'
        });
        return;
    }
    $.ajax({
        url: "SaveModify",
        dataType: "json",
        Type: "POST",
        data: {
            userGroupID: userGroupID,
            Remark: Remark,
            userGroupName: userGroupName
        },
        success: function (data) {
            var json = eval(data);
            if (json.result == "Success") {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
                $("#but_infoclose").click();
                setTimeout(function () {
                    history.go(0)
                }, 3000);
            }
            else {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
            }
        }
    });
});

//5.7 权限分配
$(".showInfo").click(function () {
//$("#sample-table-2 a.seperate11").click(function () {
    //改为下拉列表多了两个parent（）
    var id = $(this).parent().parent().parent().children().eq(1).text();
    getTree(id);
    $("#idTreeHidden").val(id);
    $("#myTreeModal").modal();
});
//5.7.1 创建权限树
function getTree(userGroupID) {
    var setting = {
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }
        }, data: {
            simpleData: {
                enable: true
            },
            key: {
                checked: "Checked"
            }
        },

        async: {
            enable: true,
            url: "ShowZTreeList?id=" + userGroupID,
            type: "post"
        }


    };

    var code;

    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
        py = $("#py").attr("checked") ? "p" : "",
        sy = $("#sy").attr("checked") ? "s" : "",
        pn = $("#pn").attr("checked") ? "p" : "",
        sn = $("#sn").attr("checked") ? "s" : "",
        type = { "Y": py + sy, "N": pn + sn };
        zTree.setting.check.chkboxType = type;
        showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
    }
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>" + str + "</li>");
    }

    $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo"), setting, null);
        //setCheck();
        //$("#py").bind("change", setCheck);
        //$("#sy").bind("change", setCheck);
        //$("#pn").bind("change", setCheck);
        //$("#sn").bind("change", setCheck);
    });
}
//5.7.2 点击保存将所选序列号以及本身角色Id传回服务器
$("#but_savetree").click(function () {
    var nodes = onCheck();
    var id = $("#idTreeHidden").val();
    $.ajax({
        url: "SaveTree",
        dataType: "json",
        data: {
            id: id,
            nodes: nodes
        },
        Type: "post",
        success: function (data) {
            var json = eval(data);
            if (json.result == "Success") {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
                $("#but_infoclose").click();
                setTimeout(function () {
                    history.go(0)
                }, 3000);

            }
            else {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
            }

        }
    });
});

//5.7.3 获取所有选中的节点
function onCheck(e, treeId, treeNode) {
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
    nodes = treeObj.getCheckedNodes(true),
    v = "";
    if (nodes.length == 0) { return ""; }
    var ids = nodes[0].id;
    for (var i = 1; i < nodes.length; i++) {
        v += nodes[i].name;
        //alert(nodes[i].id); //获取选中节点的值
        ids += "," + nodes[i].id;
    }
    return ids;

}